import ModelUtils from "@/editor/domain/utils/MoldUtils";
import { Event } from "@/editor/framework/event/Event";
import { Keyboard } from "@/editor/framework/event/Keyboard";
import { KeyBoardManager } from "@/editor/framework/event/KeyBoardManager";
import { default as BIM, default as KY3D } from "@/editor/KY3D";
import { CurMode, DrawType, SceneEvent, UIEvent } from "@/libs/const/enum";
import { KY3DEvent } from "@/libs/const/event";
import { MeshPhongMaterial, TextureLoader } from "three";
import MeshHelper from "../helper/MeshHelper";

/**
 * @description 全局事件管理类
 * @author songmy
 * 
 */
export default class EventMgr implements IMgr {

    private move_callBack: any;

    private out_callBack: any;

    private down_callBack: any;

    private up_callBack: any;

    private move2d_callBack: any;

    private out2d_callBack: any;

    private down2d_callBack: any;

    private up2d_callBack: any;


    startUp(): void {
        console.log('event mgr start up')
        this.addEvent();
    }

    dispose(): void {
        this.removeEvent();
    }

    private addEvent(): void {

        this.move_callBack = (event: PointerEvent) => {
            KY3D.ED.event(SceneEvent.SCENE_POINTE_MOVE, [event]);
        }
        this.out_callBack = (event: PointerEvent) => {
            KY3D.ED.event(SceneEvent.SCENE_POINTE_OUT, [event]);
        }
        this.down_callBack = (event: PointerEvent) => {
            KY3D.ED.event(SceneEvent.SCENE_POINTE_DOWN, [event]);
        }
        this.up_callBack = (event: PointerEvent) => {
            KY3D.ED.event(SceneEvent.SCENE_POINTE_UP, [event]);
        }
        this.move2d_callBack = (event: PointerEvent) => {
            KY3D.ED.event(SceneEvent.HUD_POINTE_MOVE, [event]);
        }
        this.out2d_callBack = (event: PointerEvent) => {
            KY3D.ED.event(SceneEvent.HUD_POINTE_OUT, [event]);
        }
        this.down2d_callBack = (event: PointerEvent) => {
            KY3D.ED.event(SceneEvent.HUD_POINTE_DOWN, [event]);
        }
        this.up2d_callBack = (event: PointerEvent) => {
            KY3D.ED.event(SceneEvent.HUD_POINTE_UP, [event]);
        }

        let cssdom = KY3D.MGR.scene.main.css2dRender.domElement;
        cssdom.addEventListener('pointermove', this.move_callBack);
        cssdom.addEventListener('pointerout', this.out_callBack);
        cssdom.addEventListener('pointerdown', this.down_callBack);
        cssdom.addEventListener('pointerup', this.up_callBack);

        let huddom = KY3D.MGR.scene.hud.render.domElement;
        huddom.addEventListener('pointermove', this.move2d_callBack);
        huddom.addEventListener('pointerout', this.out2d_callBack);
        huddom.addEventListener('pointerdown', this.down2d_callBack);
        huddom.addEventListener('pointerup', this.up2d_callBack);

        BIM.ED.on(UIEvent.LEFT_MENU_ITEM_CLICK, this, this.leftMenuClick);
        BIM.ED.on(Event.KEY_DOWN, this, this.onkeydown);
    }

    private removeEvent(): void {
        BIM.ED.off(UIEvent.LEFT_MENU_ITEM_CLICK, this, this.leftMenuClick);
        BIM.ED.off(Event.KEY_DOWN, this, this.onkeydown);
    }

    private leftMenuClick(data: string): void {
        console.log('left menu click :', data);
        switch (data) {
            case '画墙':
                KY3D.SC.mesh.drawMeshByType(DrawType.wall);
                break;
            case '横梁':
                KY3D.SC.mesh.drawMeshByType(DrawType.beam);
                break;
            case '方形柱':
                KY3D.SC.mesh.drawMeshByType(DrawType.pillar);
                break;
            case '楼板':
                KY3D.SC.mesh.drawMeshByType(DrawType.floor);
                break;
        }
    }

    private onkeydown(e: Event): void {

        let isCtrl: boolean = KeyBoardManager.hasKeyDown(Keyboard.CONTROL);
        switch (e.keyCode) {
            case Keyboard.ESCAPE:
                this.dealEscapeEvent();
                break;
            case Keyboard.V:
                KY3D.mode = KY3D.mode === CurMode.SPECT ? CurMode.DRAW : CurMode.SPECT;
                BIM.MGR.scene.changeViewMode(KY3D.mode);
                break;
            case Keyboard.M:
                this.test();
                break;
        }
    }

    private dealEscapeEvent(): void {
        if (KY3D.draw !== -1) {
            KY3D.SC.mesh.exitDrawMesh(KY3D.draw)
        }
        KY3D.ED.event(KY3DEvent.KEY_DOWN_ESC);
    }

    private test(): void {

        let scene = KY3D.MGR.scene.main.scene;

        let data: any = {
            L: 10,
            W: 4,
            H: 5
        }

        let right = './image/tex.jpg';
        let texture = new TextureLoader();
        let rightTexture = texture.load(right);

        let vi = ModelUtils.getWallVI(data);
        let entity = MeshHelper.updateModel(vi, data, '');
        (entity.material as MeshPhongMaterial).map = rightTexture;
        entity.position.set(2, 0, 0);

        scene.add(entity);

    }
}